/**
 * controller_radio.js
 * 电台相关控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-06
 */

//晨间电台
app.controller('RadioMorningIndex', function($scope, $http, $location, $timeout, common, wechat){
	//变量声明
	$scope.radioInfo 	= [];
	$scope.radioList 	= [];
	$scope.tryList 	= [];
	$scope.rankList 	= [];
	$scope.isEnd 	= false;
	$scope.tryIsEnd 	= false;
	$scope.loading 	= false;
	$scope.userInfo 	= new Object();
	$scope.nPage 	= 1;
	$scope.tryNPage 	= 1;
	$scope.listenCount 	= 1000;
	$scope.checkInfo 	= new Object();
	$scope.parentWill 	= '';

	//初始化JS
	$scope.init_js 	= function(){
		common.setTitle('沫言育儿电台');
		wechat.wxConfig();
	}

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/RadioMorning/index?code='+config.code;
		console.log(url);
		$http.get(url).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			//弹窗展示
			if(info.data.modal_show == 1){
				$('.modal-check').modal('open');
			}

			var nowTime	= Date.parse(new Date());
				nowTime	= parseInt(nowTime/1000);
			var vipEndTime 	= info.data.user_info.vip_endtime;
				vipEndTime 	= parseInt(vipEndTime);
			var surVipDays 	= (vipEndTime - nowTime)/(3600*24);

			$scope.checkInfo 	= info.data.check_info;
			$scope.surVipDays 	= Math.ceil(surVipDays);
			$scope.radioInfo 	= info.data.action_radio;
			if(!$scope.radioInfo){
				common.doAlert('无效电台');
				return false;
			}
			$scope.parentWill 	= common.trustAsHtml(false, info.data.parent_will);

			$scope.tryList 	= info.data.try_list;
			$scope.radioList 	= info.data.radio_list;
			// $scope.radioList.splice(0, 1);
			$scope.rankList 	= info.data.rank_list;
			$scope.userInfo 	= info.data.user_info;
			$scope.listenCount 	= info.data.listen_count;
			$scope.loading 	= false;

			$scope.radioInfo.author 	= '晨间电台';
			$scope.radioInfo.aplayer_btn 	= '../image/radio/btn-play.png';
			$scope.radioInfo.aplayer 	= common.initAudio(document.getElementById('box-audio'), $scope.radioInfo);
			$scope.radioInfo.aplayer.on('play', function(){
				$scope.log_action($scope.radioInfo.id);
				$scope.$apply(function(){
					$scope.radioInfo.aplayer_btn 	= '../image/radio/btn-pause.png';
				})
			})
			$scope.radioInfo.aplayer.on('pause', function(){
				$scope.$apply(function(){
					$scope.radioInfo.aplayer_btn 	= '../image/radio/btn-play.png';
				})
			})

			//用户VIP等级检测
			if($scope.userInfo.vip_level != 10){
				$scope.init_audio_list($scope.radioList);
				$scope.init_audio_list($scope.tryList);
			}
				
		})

		$scope.loading 	= true;
	}

	//加载更多电台
	$scope.load_more 	= function(isTry){
		var isTry 	= isTry ? isTry : 0;
		var page 	= isTry ? $scope.tryNPage + 1 : $scope.nPage + 1;
		var url 	= config.prefix_url + '/RadioMorning/getList?code='+config.code+
			'&p='+page;
		var param 	= {
			'condition[is_try]' : isTry
		}
		
		$http.get(url + '&' + objectToHttp(param)).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			if(isTry){
				$scope.tryIsEnd 	= common.dealListInfo(info.data.list, $scope.tryList, page);
				$scope.tryNPage 	= page;
			}else{
				$scope.isEnd 	= common.dealListInfo(info.data.list, $scope.radioList, page);
				$scope.nPage 	= page;
			}
			$scope.loading 	= false;

			$scope.init_audio_list(info.data.list);	
		})

		$scope.loading 	= true;
	}

	//初始化音频列表
	$scope.init_audio_list 	= function(audioList){
		this.init_audio 	= function(element, audioInfo){
			// console.log(audioInfo);
			var onlineDate 	= audioInfo.online_date ? audioInfo.online_date : audioInfo.create_date;
			var audio 	= new APlayer({
		        element: element,
		        narrow: false,
		        showlrc: false,
		        // preload	: 'none',
		        music: {
		            title: audioInfo.title,
		            author: onlineDate,
		            url: audioInfo.audio,
		            pic: '../image/radio/logo-doubao.png'
		        }
			});

			audio.on('play', function(){
				$scope.log_action(audioInfo.id);
			});
		}

		var that 	= this;

		$timeout(function(){
			for(var k in audioList){
				var audioInfo 	= audioList[k];
				var element 	= document.getElementById('aplayer-'+audioInfo.id);
				$timeout(that.init_audio(element, audioInfo), 300);
			}
		}, config.dom_delay);
		
	}

	//播放动作记录
	$scope.log_action 	= function(radioId){
		var url  	= config.prefix_url + '/RadioMorning/logAction?code='+config.code+
			'&radio_id='+radioId;

		$http.get(url).success(function(info){
			console.log(info);
		});
	}

	//播放控制
	$scope.toggle_aplayer 	= function(aplayer){
		aplayer.toggle();
	}

	//购买会员
	$scope.purchase_vip 	= function(){
		common.logAnchor();
		$location.path('/Vip/purchase');
	}

	//邀请好友
	$scope.share 	= function(){
		$location.path('/User/2dcode');
	}

	$scope.load_data();
	$scope.init_js();
})

//晚间电台
app.controller('RadioEveningIndex', function($scope, $http, $location, $timeout, common, wechat){
	$scope.radioInfo 	= [];
	$scope.storyList 	= [];
	$scope.loading 	= false;
	$scope.isEnd 	= false;
	$scope.nPage 	= 1;
	$scope.listenCount 	= 1000;
	$scope.checkInfo 	= new Object();
	$scope.parentWill 	= '';

	//初始化JS
	$scope.init_js 	= function(){
		common.setTitle('晚间电台');
		wechat.wxConfig();
	}

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/RadioEvening/index?code='+config.code;

		$http.get(url).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			//弹窗展示
			if(info.data.modal_show == 1){
				$('.modal-check').modal('open');
			}

			var nowTime	= Date.parse(new Date());
				nowTime	= parseInt(nowTime/1000);
			var vipEndTime 	= info.data.user_info.vip_endtime;
				vipEndTime 	= parseInt(vipEndTime);
			var surVipDays 	= (vipEndTime - nowTime)/(3600*24);
			
			$scope.checkInfo 	= info.data.check_info;
			$scope.surVipDays 	= Math.ceil(surVipDays);
			$scope.radioInfo 	= info.data.radio_info;
			$scope.storyList 	= info.data.story_list;
			$scope.listenCount 	= info.data.listen_count;
			$scope.userInfo 	= info.data.user_info;
			$scope.parentWill 	= common.trustAsHtml(false, info.data.parent_will);

			var btnPlay 	= '../image/radio/btn-play.png';
			var btnPause 	= '../image/radio/btn-pause.png';
			//声音插件初始化
			$scope.radioInfo.author 	= '晚间电台';
			$scope.radioInfo.aplayer 	= common.initAudio(document.getElementById('box-audio'), {
				'title'	: $scope.radioInfo.title,
				'author'	: '声音I',
				'audio'	: $scope.radioInfo.audio
			}, btnPlay)
			$scope.radioInfo.aplayer.on('play', function(){
				//使用jquery改变插件图片
				$('#box-audio').find('.aplayer-pic').css('background-image', 'url("'+btnPause+'")');
			})
			$scope.radioInfo.aplayer.on('pause', function(){
				//使用jquery改变插件图片
				$('#box-audio').find('.aplayer-pic').css('background-image', 'url("'+btnPlay+'")');
			})


			$scope.radioInfo.aplayer_ii 	= common.initAudio(document.getElementById('box-audio-ii'), {
				'title'		: $scope.radioInfo.title_ii,
				'author'	: '声音II',
				'audio'		: $scope.radioInfo.audio_ii
			}, btnPlay)
			$scope.radioInfo.aplayer_ii.on('play', function(){
				//使用Jquery改变播放图片
				$('#box-audio-ii').find('.aplayer-pic').css('background-image', 'url("'+btnPause+'")');
			})
			$scope.radioInfo.aplayer_ii.on('pause', function(){
				$('#box-audio-ii').find('.aplayer-pic').css('background-image', 'url("'+btnPlay+'")');
			})

			$scope.init_audio_list($scope.storyList);
		})

		$scope.loading 	= true;
	}

	//初始化音频列表
	$scope.init_audio_list 	= function(audioList){
		this.init_audio 	= function(element, audioInfo){
			var onlineDate 	= audioInfo.online_date ? audioInfo.online_date : audioInfo.create_date;
			var audio 	= new APlayer({
		        element: element,
		        narrow: false,
		        showlrc: false,
		        // preload	: 'none',
		        music: {
		            title: audioInfo.title,
		            author: onlineDate,
		            url: audioInfo.audio,
		            pic: '../image/radio/logo-doubao.png'
		        }
			});

			// audio.on('play', function(){
			// 	$scope.log_action(audioInfo.id);
			// });
		}

		var that 	= this;

		$timeout(function(){
			for(var k in audioList){
				var audioInfo 	= audioList[k];
				var element 	= document.getElementById('aplayer-'+audioInfo.id);
				$timeout(that.init_audio(element, audioInfo), 300);
			}
		}, config.dom_delay);
			
	}

	//加载更多数据
	$scope.load_more 	= function(){
		var page 	= $scope.nPage + 1;
		var url 	= config.prefix_url + '/RadioEvening/getStoryList?code='+config.code+
			'&p='+page;

		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.isEnd 	= common.dealListInfo(info.data.list, $scope.storyList, page);
			$scope.nPage 	= page;
			$scope.loading 	= false;
			$scope.init_audio_list(info.data.list);
		})

		$scope.loading 	= true;
	}

	//播放动作记录
	$scope.log_action 	= function(tarType, radioId){
		var url 	= config.prefix_url + '/RadioMorning/logAction?code='+config.code+
			'&target_type='+tarType+
			'&radio_id='+radioId;

		$http.get(url).success(function(info){console.log(info)});
	}

	//购买会员
	$scope.purchase_vip 	= function(){
		common.logAnchor();
		$location.path('/Vip/purchase');
	}

	//播放控制
	$scope.toggle_aplayer 	= function(aplayer){
		aplayer.toggle();
	}

	//邀请好友
	$scope.share 	= function(){
		$location.path('/User/2dcode');
	}

	$scope.load_data();
	$scope.init_js();
})
